/************************************************************************************/
/*  Program name CAPFAIMP_BOOKS.sql                                        			*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 16 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FA_BOOKS_ARC1 DISABLE;
ALTER TRIGGER FA_BOOKS_ARC2 DISABLE;
ALTER TRIGGER JL_ZZ_FA_BOOKS_GLOB_FLEX_RL DISABLE;
ALTER TRIGGER JL_ZZ_FA_BOOKS_GLOB_FLEX_SL DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_BOOKS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_BOOKS;

BEGIN
	UPDATE
		FA_BOOKS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_BOOKS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN		
			INSERT INTO FA_BOOKS
			VALUES
			(
			C1.BOOK_TYPE_CODE,
			CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID),
			C1.DATE_PLACED_IN_SERVICE,
			C1.DATE_EFFECTIVE,
			C1.DEPRN_START_DATE,
			C1.DEPRN_METHOD_CODE,
			C1.LIFE_IN_MONTHS,
			C1.RATE_ADJUSTMENT_FACTOR,
			C1.ADJUSTED_COST,
			C1.COST,
			C1.ORIGINAL_COST,
			C1.SALVAGE_VALUE,
			C1.PRORATE_CONVENTION_CODE,
			C1.PRORATE_DATE,
			C1.COST_CHANGE_FLAG,
			C1.ADJUSTMENT_REQUIRED_STATUS,
			C1.CAPITALIZE_FLAG,
			C1.RETIREMENT_PENDING_FLAG,
			C1.DEPRECIATE_FLAG,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.DATE_INEFFECTIVE,
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSACTION_HEADER_ID_IN),
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSACTION_HEADER_ID_OUT),
			C1.ITC_AMOUNT_ID,
			C1.ITC_AMOUNT,
			CAPBRIMP_MAPPING.GetRetirementIDMapping(C1.RETIREMENT_ID),
			C1.TAX_REQUEST_ID,
			C1.ITC_BASIS,
			C1.BASIC_RATE,
			C1.ADJUSTED_RATE,
			C1.BONUS_RULE,
			C1.CEILING_NAME,
			C1.RECOVERABLE_COST,
			C1.LAST_UPDATE_LOGIN,
			C1.ADJUSTED_CAPACITY,
			C1.FULLY_RSVD_REVALS_COUNTER,
			C1.IDLED_FLAG,
			C1.PERIOD_COUNTER_CAPITALIZED,
			C1.PERIOD_COUNTER_FULLY_RESERVED,
			C1.PERIOD_COUNTER_FULLY_RETIRED,
			C1.PRODUCTION_CAPACITY,
			C1.REVAL_AMORTIZATION_BASIS,
			C1.REVAL_CEILING,
			C1.UNIT_OF_MEASURE,
			C1.UNREVALUED_COST,
			C1.ANNUAL_DEPRN_ROUNDING_FLAG,
			C1.PERCENT_SALVAGE_VALUE,
			C1.ALLOWED_DEPRN_LIMIT,
			C1.ALLOWED_DEPRN_LIMIT_AMOUNT,
			C1.PERIOD_COUNTER_LIFE_COMPLETE,
			C1.ADJUSTED_RECOVERABLE_COST,
			C1.ANNUAL_ROUNDING_FLAG,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.EOFY_ADJ_COST,
			C1.EOFY_FORMULA_FACTOR,
			C1.SHORT_FISCAL_YEAR_FLAG,
			C1.CONVERSION_DATE,
			C1.ORIGINAL_DEPRN_START_DATE,
			C1.REMAINING_LIFE1,
			C1.REMAINING_LIFE2,
			C1.OLD_ADJUSTED_COST,
			C1.FORMULA_FACTOR,
			C1.GROUP_ASSET_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER FA_BOOKS_ARC1 ENABLE;
ALTER TRIGGER FA_BOOKS_ARC2 ENABLE;
ALTER TRIGGER JL_ZZ_FA_BOOKS_GLOB_FLEX_RL ENABLE;
ALTER TRIGGER JL_ZZ_FA_BOOKS_GLOB_FLEX_SL ENABLE;